import { setCookie, getCookie } from 'h3';
// import { useLogger } from '@nuxt/kit'
import { inspect } from 'node:util';

export default defineEventHandler(async (event) => {
  // const logger = useLogger('my-module');

  // logger.info('Hello from my module!');

  const uuid = getCookie(event, "uuid");
  event.context.auth = { user: 123 }
  if (uuid) {
    // console.log('already uuid:', uuid ?? '');
    return;
  }
  try {
    const { data } = await fetch(import.meta.env.VITE_API + '/auth/visitor/uuid').then(res => res.json());
    // console.log('refresh uuid:', inspect(data));
    setCookie(event, 'uuid', data.unique_visitor_id, {
      expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 30),
      domain: '.petslib.cn',
      sameSite: undefined,
      path: '/',
      httpOnly: false
    });
  } catch (error) {
    // console.log('refresh uuid error:', error);
  }
});